package com.huiquan.analysis.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.domain.User;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * ClassName:Constants Description:
 *
 * @author ChaoKai Wen email:wenchaokai@gmail.com
 * @Date 14/9/22
 * @since Ver 1.0
 */
@Repository("userDao")
public class UserDao extends BaseDao {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "BAS_USER_SPACE.";

	public User retrieveUser(String userName, String password) {

		Map<String, Object> map = new HashMap<>();
		map.put("userName", userName);
		map.put("password", password);
		User user = (User) this.object(space + "RETRIEVE_USER", map, sqlMapClient);

		return user;
	}

	@SuppressWarnings("unchecked")
	public List<User> retrieveBelongUsers(Integer userId) {

		Map<String, Object> params = new HashMap<>();
		params.put("parentId", userId);

		return (List<User>) this.list(space + "LIST_BELONG_USERS", params, sqlMapClient);
	}

	public void insertUser(Integer userId, String userName, String password, String realName, Integer parentId,
			Integer role) {
		Map<String, Object> params = new HashMap<>();
		params.put("uid", userId);
		params.put("userName", userName);
		params.put("password", password);
		params.put("realName", realName);
		params.put("parentId", parentId);
		params.put("role", role);
		this.insert(space + "INSERT_USER", params, sqlMapClient);
	}

	public int retrieveUidByUname(String uname) {
		return (int) this.object(space + "RETRIEVE_UID_BY_UNAME", uname, sqlMapClient);
	}

	public String retrieveUnameByUid(Long uid) {
		return (String) this.object(space + "RETRIEVE_UNAME_BY_UID", uid, sqlMapClient);
	}

	public User retrieveObjectById(int id) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);

		return (User) this.object(space + "RETRIEVE_OBJECT_BY_ID", map, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<String> getRolePeopleNum(String role){
		Map<String, Object> params = new HashMap<>();
		params.put("role", role);

		return (List<String>) this.list(space + "RETRIEVE_ROLE_NUM", params, sqlMapClient);
	}
	@SuppressWarnings("unchecked")
	public List<String> retrieveUsersByRole(Long role) {
		return (List<String >) this.list(space + "RETRIEVE_UID_BY_ROLE", role, sqlMapClient);
	}
}
